<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>设备管理</title>
	<link href="../css/public.css" rel="stylesheet"/>
	<link href="../css/main.css" rel="stylesheet"/>
	<link href="../css/style.css" rel="stylesheet"/>
	<link href="../css/agent.css?v=1" rel="stylesheet"/>
	<script src="http://www.jq22.com/jquery/jquery-1.9.1.js"></script>
	<script src="../js/swiper.min.js"></script>
	<script src="../js/mobile-util.js"></script>
	<script src="../js/public.js"></script>
	<script src="../js/layer/layui.js"></script>
	<script src="../js/jquery.cookie.js"></script>

	<script>
		/*获取url参数*/
		function getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
			var r = window.location.search.substr(1).match(reg);  //匹配目标参数
			if (r != null) return unescape(r[2]); return null; //返回参数值
		}

		/*获取url参数end*/
	</script>

</head>
<body >

<header class="mui-bar mui-bar-nav indexheader bluebg flex">
	<div class="headerlogo" onclick="back()">
		<i class="iconfont backbtn">&#xe602;</i>
	</div>

	<div class="headertitle">
		<p>设备管理</p>
	</div>

	<div class="headerright timeright hide">
		<p class="timepbox iconfont">&#xe6d9;</p>
	</div>

</header>

<main class="agentbox">

	<div class="tit-flash-sale p mytit_flash">

			<ul class="mydevice">
				<li class="co-deepblue" onclick="device(this,0)" data-type="0">
					<a href="javascript:void(0)" class="tab_head">设备总数(<em id="alldevice"></em>台)</a>
				</li>

				<li id="WAITPAY" onclick="device(this,1)" data-type="1">
				<a href="javascript:void(0)" class="tab_head">未激活(<em id="devicejihuo"></em>台)</a>
				</li>

				<li id="WAITSEND" onclick="device(this,2)" data-type="2">
					<a href="javascript:void(0)" class="tab_head">已激活(<em id="divecenull"></em>台)</a>
				</li>

			</ul>

	</div>


	<div class="devicebox">

		<!--设备总数-->
		<div class="devicelistbox" id="total">
			<div class="flex devicehead">
				<div class="flex-1">设备号</div>
				<div class="flex-1">状态</div>
				<div class="flex-1">添加时间</div>
			</div>

			<div class="nulldecice">
				<p class="iconfont ">&#xe6ed;</p>
				<p class="nulldevicetitle">您还没有wifi设备</p>
			</div>

			<div class="deviceinfo" id="totallist">


				<!--<div class="flex devicelist">-->
					<!--<div class="flex-1">-->
						<!--1111111-->
					<!--</div>-->
					<!--<div class="flex-1">-->
						<!--三个月包月套餐-->
					<!--</div>-->
					<!--<div class="flex-1">-->
						<!--2018-1-1-->
					<!--</div>-->
				<!--</div>-->


			</div>

			<div class="businessmore hide" onclick="ajax_device_submit()">
				<p>查看等多</p>
				<p class="companyicon businessmoreicon">
					<i class="iconfont comicon">&#xe624;</i>
				</p>
			</div>

		</div>

		<!--设备总数end-->

		<!--未分配设备-->
		<div class="devicelistbox" id="activation">
			<div class="flex devicehead">
				<div class="flex-1">
					<p class="checkboxpre">
						<input id="checkbox" type="checkbox"  value="1" class="checkbox1">全选
						<label  for="checkbox" class="seradiobox  iconfont">&#xe648;</label>
					</p>
				</div>

				<div class="flex-1">设备号</div>
				<div class="flex-1">状态</div>
				<div class="flex-1">添加时间</div>
			</div>

			<div class="nulldecice">
				<p class="iconfont ">&#xe6ed;</p>
				<p class="nulldevicetitle">您还没有wifi设备</p>
			</div>

			<div class="deviceinfo" id="activationlist">



				<!--<div class="flex devicelist">-->
					<!--<div class="flex-1">-->
						<!--<p class="checkboxpre">-->
							<!--<input id="checkbox3" type="checkbox"  value="1" class="checkbox">-->
							<!--<label  for="checkbox3" class="seradiobox checklabel iconfont">&#xe648;</label>-->
						<!--</p>-->
					<!--</div>-->
					<!--<div class="flex-1">-->
						<!--1111111-->
					<!--</div>-->
					<!--<div class="flex-1">-->
						<!--三个月包月套餐-->
					<!--</div>-->
					<!--<div class="flex-1">-->
						<!--2018-1-1-->
					<!--</div>-->
				<!--</div>-->


			</div>
			<div class="flex distributionbox">

			</div>




			<div class="businessmore hide" onclick="ajax_devicejihuo_submit()">
				<p>查看等多</p>
				<p class="companyicon businessmoreicon">
					<i class="iconfont comicon">&#xe624;</i>
				</p>
			</div>

		</div>
		<!--已激活设备设备end-->

		<!--已分配设备-->
		<div class="devicelistbox" id="notactive">
			<div class="flex devicehead">
				<div class="flex-1">设备号</div>
				<div class="flex-1">状态</div>
				<div class="flex-1">姓名</div>
				<div class="flex-1">分配时间</div>
			</div>

			<div class="nulldecice">
				<p class="iconfont ">&#xe6ed;</p>
				<p class="nulldevicetitle">您还没有wifi设备</p>
			</div>

			<div class="deviceinfo" id="notactivelist">
				<!--<div class="flex devicelist">-->
					<!--<div class="flex-1">-->
						<!--1111111-->
					<!--</div>-->
					<!--<div class="flex-1">-->
						<!--未激活-->
					<!--</div>-->

				<!--</div>-->


			</div>

			<div class="businessmore hide" onclick="ajax_deviceactivelist_submit()">
				<p>查看等多</p>
				<p class="companyicon businessmoreicon">
					<i class="iconfont comicon">&#xe624;</i>
				</p>
			</div>


		</div>
		<!--未激活设备设备end-->



	</div>



</main>


<script>

	var tooken=$.cookie("tooken");


	if(tooken==null||tooken==undefined||tooken==""){

		window.location.href="../agent/login.html";

	}else {
		/*全选*/
		$('#checkbox').click(function () {
			if($(this).is(":checked")){
				$('.checkbox').attr("checked","true");
				$('.checklabel').addClass("blueioactive");
				$('.checklabel').html("&#xe608;");
				$(this).next("label").addClass("blueioactive");
				$(this).next("label").html("&#xe608;");
			}else {
				$('.checkbox').removeAttr("checked");
				$('.checklabel').removeClass("blueioactive");
				$('.checklabel').html("&#xe648");

				$(this).next("label").removeClass("blueioactive");
				$(this).next("label").html("&#xe648;");
			}
		});
		/*全选*/





		var name=$.cookie("tooken");
		var page=1;

		var url="http://ht.hiberbest.com";
		//var url="http://192.168.0.132:69";




		function distribution() {
			var arr=[];
			$('.checkbox').each(function () {
				if($(this).is(":checked")){

					var val=$(this).val();
					arr.push(val);


				}

			});
			//console.log(arr);

			var  agentid=$('#agentid').val();
			var geturl=url + "/admin/deviceapi/deviceToAgent2";

			$.ajax({
				type: 'post',
				dataType: "json",
				data:{
					"tooken":name,
					"agentid":agentid,
					"devicesnList":arr
				},
				url: geturl,
				success:function (data) {
					var suc=data.success;
					if(suc.code == 4008){
						alert(suc.message);
					}else if(suc.code == 4000){
						window.location.href='levelmydevice.html?name='+name;
					}

					console.log(data);
				},error:function () {
					console.log("请检查您的网络");
				}
			})


		}


		getAjaxDevice(0);


		function getAjaxDevice(type){

			if(type == 0 ){
				var geturl=url+ "/Admin/Deviceapi/getSelfDeviceList?tooken="+name+"&page=1&limit=10";

				$.ajax({
					type: 'get',
					dataType: "json",
					url: geturl,
					success: function (data) {
						// console.log(data);

						var count=data.count;
						var jihuo=count.jihuo;
						var all=count.count;
						var weifenpei=count.weifenpei;
						var nulldevice=all-weifenpei;
						var feipei=count.fenpei;
						$('#alldevice').html(all);
						$('#devicejihuo').html(weifenpei);
						$('#divecenull').html(nulldevice);

						var suc = data.success;
						if (suc.code == 4007) {
							alert(suc.message);
							//window.location.href="login.html";
						} else if (suc.code == 4011) {
							alert(suc.message);
							//window.location.href = "login.html";
						} else if (suc.code == 4000) {
							var deviceList=data.deviceList;
							var str="";

							if(deviceList.length == 0){
								$('#totallist').siblings(".nulldecice").show();
								return false;
							}

							if(deviceList.length >= 5){
								$('#totallist').siblings(".businessmore").removeClass("hide");
							}

							for(var i=0;i<deviceList.length;i++){
								var status="";
								if(deviceList[i].agent2==null&&deviceList[i].jihuo==0){
									status="未分配";
								}else {
									status=deviceList[i].agent2name;
								}

								console.log(status);

								str+='<div class="flex devicelist">' +
										'<div class="flex-1">' +
										''+deviceList[i].device_sn+'' +
										'</div>' +
										'<div class="flex-1">' +
										''+status+'' +
										'</div>' +
										'<div class="flex-1">' +
										''+deviceList[i].addtime+'' +
										'</div>' +
										'</div>'
							}



							$('#totallist').html(str);
							$('#totallist').siblings(".nulldecice").hide();
						}
					}

				});
			}else if(type == 1){
				var fenpei=0;
				var geturl=url+ "/Admin/Deviceapi/getSelfDeviceList?tooken="+name+"&page=1&limit=10"+"&fenpei="+fenpei;

				$.ajax({
					type: 'get',
					dataType: "json",
					url: geturl,
					success: function (data) {
						//console.log(data);
						var suc = data.success;
						if (suc.code == 4007) {
							alert(suc.message);
							//window.location.href="login.html";
						} else if (suc.code == 4011) {
							alert(suc.message);
							//window.location.href = "login.html";
						} else if (suc.code == 4000) {
							var deviceList=data.deviceList;
							var str="";

							if(deviceList.length == 0){
								$('#activationlist').siblings(".nulldecice").show();
								return false;
							}

							if(deviceList.length >= 5){
								$('#activationlist').siblings(".businessmore").removeClass("hide");
							}

							for(var i=0;i<deviceList.length;i++){
								var status="";
								
								if(deviceList[i].agent2==null&&deviceList[i].jihuo==0){
									status="未分配";
								}else {
									status=deviceList[i].agent2name;
								}
								str+='<div class="flex devicelist">' +
										'<div class="flex-1">' +
										'<p class="checkboxpre">' +
										'<input id="checkbox'+ i +'" type="checkbox"  value="'+ deviceList[i].device_sn +'" class="checkbox">' +
										'<label  for="checkbox'+ i +'" class="seradiobox checklabel iconfont">&#xe648;</label>' +
										'</p>' +
										'</div>'+
										'<div class="flex-1">' +
										''+deviceList[i].device_sn+'' +
										'</div>' +
										'<div class="flex-1">' +
										status +
										'</div>' +

										'<div class="flex-1">' +
										''+deviceList[i].addtime+'' +
										'</div>' +
										'</div>'
							}

							$('#activationlist').html(str);
							$('#activationlist').siblings(".nulldecice").hide();


							/*多选框选择*/

							$('.checkbox').change(function () {
								if($(this).is(":checked")){
									$(this).next(".blueioactive:not(:checked)").html("&#xe648;");
									$(this).next(".blueioactive:not(:checked)").removeClass("blueioactive");
									$(this).next("label").addClass("blueioactive");
									$(this).next("label").html("&#xe608;");
								}else{
									$(this).next(".blueioactive:not(:checked)").html("&#xe608;");
									$(this).next(".blueioactive:not(:checked)").addClass("blueioactive");
									$(this).next("label").removeClass("blueioactive");
									$(this).next("label").html("&#xe648;");
								}
							});


							/*多选框选择end*/

							var geturl1=url + "/admin/agentapi/agent2List?tooken="+name;
							$.ajax({
								type: 'get',
								dataType: "json",
								url: geturl1,
								success: function (data) {
									console.log(data);
									var agent=data.agent;

									if(agent.length==0){
										alert("您还没有添加二级代理");
										window.location.href='addagent.html';
									}

									var select="";
									for(var i=0;i<agent.length;i++){
										select+='<option value="'+ agent[i].id +'" data-id="'+ agent[i].id +'">'+ agent[i].agent_name+'</option>'
									}

									var str1= '<div class="flex-1">分配给:</div>' +
											'<div class="flex-1">' +
											'<select id="agentid">' +
											''+ select +'' +
											'</select>' +
											'</div>' +
											'<div class="flex-1 te-ce">' +
											'<button class="distribtn" onclick="distribution()">' +
											'确定' +
											'</button>' +
											'</div>' ;

									$('.distributionbox').html(str1);






								}, error: function () {
									console.log("请检查您的网络");
								}
							});


						}
					}

				});
			}else if(type == 2){
				var fenpei=1;
				var geturl=url+ "/Admin/Deviceapi/getSelfDeviceList?tooken="+name+"&page=1&limit=10"+"&fenpei="+fenpei;

				$.ajax({
					type: 'get',
					dataType: "json",
					url: geturl,
					success: function (data) {
						//console.log(data);
						var suc = data.success;
						if (suc.code == 4007) {
							alert(suc.message);
							//window.location.href="login.html";
						} else if (suc.code == 4011) {
							alert(suc.message);
							//window.location.href = "login.html";
						} else if (suc.code == 4000) {
							var deviceList=data.deviceList;
							var str="";

							if(deviceList.length == 0){
								$('#notactivelist').siblings(".nulldecice").show();
								return false;
							}
							if(deviceList.length >= 5){
								$('#notactivelist').siblings(".businessmore").removeClass("hide");
							}

							for(var i=0;i<deviceList.length;i++){
								str+='<div class="flex devicelist">' +
										'<div class="flex-1">' +
										''+deviceList[i].device_sn+'' +
										'</div>' +
										'<div class="flex-1">' +
										'已分配' +
										'</div>' +
										'<div class="flex-1">' +
										''+deviceList[i].agent2name+'' +
										'</div>' +
										'<div class="flex-1">' +
										''+deviceList[i].fptime+'' +
										'</div>' +
										'</div>'
							}


							$('#notactivelist').html(str);
							$('#notactivelist').siblings(".nulldecice").hide();
						}
					}

				});
			}



		}




		/*加载更多*/

		/*全部设备*/
		function ajax_device_submit() {

			page += 1;

			//console.log(page);
			$.ajax({
				type : "GET",
				dataType: "json",
				url:url + "/Admin/Deviceapi/getSelfDeviceList?tooken="+name+"&page="+page,
				success: function(data) {

					var deviceList=data.deviceList;


					if(deviceList.length == 0){
						layui.use('layer', function(){
							var layer = layui.layer;

							layer.msg('没有更多数据了');
						});

					}else{

						var str="";
						for(var i=0;i<deviceList.length;i++){

							var status="";

							if(deviceList[i].agent2==null){
								status="未分配";
							}else {
								status=deviceList[i].agent2name;
							}
							console.log(deviceList[i].agent2);

							str+='<div class="flex devicelist">' +
									'<div class="flex-1">' +
									''+deviceList[i].device_sn+'' +
									'</div>' +
									'<div class="flex-1">' +
									''+status+'' +
									'</div>' +
									'<div class="flex-1">' +
									''+deviceList[i].addtime+'' +
									'</div>' +
									'</div>'

						}


						$("#totallist").append(str);

					}
				}
			});
		}


		/*未分配设备*/

		function ajax_devicejihuo_submit() {

			page += 1;
			var fenpei=0;

			$.ajax({
				type : "GET",
				dataType: "json",
				url:url + "/Admin/Deviceapi/getSelfDeviceList?tooken="+name+"&page="+page+"&limit=10"+"&fenpei="+fenpei,
				success: function(data) {
					//console.log(data);

					var deviceList=data.deviceList;


					if(deviceList.length == 0){
						layui.use('layer', function(){
							var layer = layui.layer;

							layer.msg('没有更多数据了');
						});

					}else{

						var str="";
						for(var i=0;i<deviceList.length;i++){
							var num=5*(page-1)+i;
							var status="";							
							if(deviceList[i].agent2==null&&deviceList[i].jihuo==0){
								status="未分配";
							}else {
								status=deviceList[i].agent2name;
							}
							str+='<div class="flex devicelist">' +
									'<div class="flex-1">' +
									'<p class="checkboxpre">' +
									'<input id="checkbox'+ num +'" type="checkbox"  value="'+ deviceList[i].device_sn +'" class="checkbox">' +
									'<label  for="checkbox'+ num +'" class="seradiobox checklabel iconfont">&#xe648;</label>' +
									'</p>' +
									'</div>'+
									'<div class="flex-1">' +
									''+deviceList[i].device_sn+'' +
									'</div>' +
									'<div class="flex-1">' +
									status +
									'</div>' +
									'<div class="flex-1">' +
									''+deviceList[i].addtime+'' +
									'</div>' +
									'</div>'

						}




						$("#activationlist").append(str);

						/*多选框选择*/

						$('.checkbox').change(function () {
							if($(this).is(":checked")){
								$(this).next(".blueioactive:not(:checked)").html("&#xe648;");
								$(this).next(".blueioactive:not(:checked)").removeClass("blueioactive");
								$(this).next("label").addClass("blueioactive");
								$(this).next("label").html("&#xe608;");
							}else{
								$(this).next(".blueioactive:not(:checked)").html("&#xe608;");
								$(this).next(".blueioactive:not(:checked)").addClass("blueioactive");
								$(this).next("label").removeClass("blueioactive");
								$(this).next("label").html("&#xe648;");
							}
						});


						/*多选框选择end*/

					}
				}
			});
		}


		/*已分配设备*/

		function ajax_deviceactivelist_submit() {

			page += 1;
			var fenpei=1;

			$.ajax({
				type : "GET",
				dataType: "json",
				url:url + "/Admin/Deviceapi/getSelfDeviceList?tooken="+name+"&page="+page+"&fenpei="+fenpei,
				success: function(data) {

					var deviceList=data.deviceList;


					if(deviceList.length == 0){
						layui.use('layer', function(){
							var layer = layui.layer;

							layer.msg('没有更多数据了');
						});

					}else{

						var str="";
						for(var i=0;i<deviceList.length;i++){

							str+='<div class="flex devicelist">' +
									'<div class="flex-1">' +
									''+deviceList[i].device_sn+'' +
									'</div>' +
									'<div class="flex-1">' +
									'已分配' +
									'</div>' +
									'<div class="flex-1">' +
									''+deviceList[i].agent2name+'' +
									'</div>' +
									'<div class="flex-1">' +
									''+deviceList[i].fptime+'' +
									'</div>' +
									'</div>'

						}


						$("#notactivelist").append(str);

					}
				}
			});
		}






		function device(e,type){
			if(type == 0){
				$(e).addClass("co-deepblue").siblings().removeClass("co-deepblue");
				getAjaxDevice(0);
				$('#total').show();
				page=1;
				$('#activation,#notactive').hide();
			}else if(type==1){
				$(e).addClass("co-deepblue").siblings().removeClass("co-deepblue");
				getAjaxDevice(1);
				$('#activation').show();
				page=1;
				$('#total,#notactive').hide();
			}else if(type==2){
				$(e).addClass("co-deepblue").siblings().removeClass("co-deepblue");
				getAjaxDevice(2);
				$('#notactive').show();
				page=1;
				$('#total,#activation').hide();
			}
		}


	}







</script>


</body>
</html>
